package com.japanesee_learning.activity;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.japanesee_learning.slidemenu.CollapseAnimation;
import com.japanesee_learning.slidemenu.ExpandAnimation;
import com.japanesee_learning.until.ConstantsUntil;
import com.japanesee_learning.until.ImagesUntil;
import com.japanesee_learning.until.MessageUntil;
import com.japanesee_learning.until.OnSwipeTouchUntil;

public class MainActivity extends Activity implements OnClickListener {

	// Declare
	private String TAG = MainActivity.class.getSimpleName();
	private LinearLayout slidingPanel;
	private boolean isExpanded;
	private DisplayMetrics metrics;
	private ListView listView;
	private RelativeLayout headerPanel;
	private RelativeLayout menuPanel;
	private int panelWidth;
	private ImageView menuViewButton;
	private ImagesUntil mObjImage;
	private Context mContext;
	private Button btnKatakama, btnHiragana, btnNhkWorld, btnSakura,
			btnVocabulary, btnDictionary, btnAccount, btnAboutUs, btnLogout,
			btnVerbs, btnAdjs;
	private TextView txtHeader;
	private MessageUntil mObjMessage;

	FrameLayout.LayoutParams menuPanelParameters;
	FrameLayout.LayoutParams slidingPanelParameters;
	LinearLayout.LayoutParams headerPanelParameters;
	LinearLayout.LayoutParams listViewParameters;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_main);
			getWindow().setSoftInputMode(
					WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

			/**
			 * ================================================================
			 * ==== == Initialize
			 * **/
			mContext = this;
			mObjImage = new ImagesUntil(mContext);
			mObjMessage = new MessageUntil(mContext);
			metrics = new DisplayMetrics();
			getWindowManager().getDefaultDisplay().getMetrics(metrics);

			/**
			 * ================================================================
			 * ==== == Initiliaze object
			 * **/
			btnKatakama = (Button) findViewById(R.id.btnMenuKatakana);
			btnHiragana = (Button) findViewById(R.id.btnMenuHiragana);
			btnNhkWorld = (Button) findViewById(R.id.btnMenuNhkWorld);
			btnSakura = (Button) findViewById(R.id.btnMenuSakura);
			btnVocabulary = (Button) findViewById(R.id.btnMenuVocabulary);
			btnDictionary = (Button) findViewById(R.id.btnMenuDictionary);
			btnAccount = (Button) findViewById(R.id.btnMenuAccount);
			btnAboutUs = (Button) findViewById(R.id.btnMenuAbout);
			btnLogout = (Button) findViewById(R.id.btnMenuLogout);
			btnVerbs = (Button) findViewById(R.id.btnMenuVerb);
			btnAdjs = (Button) findViewById(R.id.btnMenuAdj);
			txtHeader = (TextView) findViewById(R.id.txtHeaderTitle);
			txtHeader.setText(R.string.nhkworld);
			headerPanel = (RelativeLayout) findViewById(R.id.header);
			menuPanel = (RelativeLayout) findViewById(R.id.menuPanel);
			slidingPanel = (LinearLayout) findViewById(R.id.slidingPanel);
			menuViewButton = (ImageView) findViewById(R.id.menuViewButton);
			listView = (ListView) findViewById(R.id.lvMainList);
			enableDisableView(menuPanel, false);

			/**
			 * ================================================================
			 * ==== == Setting for menu slide
			 * **/
			panelWidth = (int) ((metrics.widthPixels) * 0.82);
			headerPanelParameters = (LinearLayout.LayoutParams) headerPanel
					.getLayoutParams();
			headerPanelParameters.width = metrics.widthPixels;
			headerPanel.setLayoutParams(headerPanelParameters);
			menuPanelParameters = (FrameLayout.LayoutParams) menuPanel
					.getLayoutParams();
			menuPanelParameters.width = panelWidth;
			menuPanel.setLayoutParams(menuPanelParameters);
			slidingPanelParameters = (FrameLayout.LayoutParams) slidingPanel
					.getLayoutParams();
			slidingPanelParameters.width = metrics.widthPixels;
			slidingPanel.setLayoutParams(slidingPanelParameters);
			listViewParameters = (LinearLayout.LayoutParams) listView
					.getLayoutParams();
			listViewParameters.width = metrics.widthPixels;
			listView.setLayoutParams(listViewParameters);

			// Slide the Panel
			menuViewButton.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					if (!isExpanded) {
						isExpanded = true;
						enableDisableView(menuPanel, true);

						// Expand
						new ExpandAnimation(slidingPanel, panelWidth,
								Animation.RELATIVE_TO_SELF, 0.0f,
								Animation.RELATIVE_TO_SELF, 0.75f, 0, 0.0f, 0,
								0.0f);
					} else {
						isExpanded = false;
						enableDisableView(menuPanel, false);

						// Collapse
						new CollapseAnimation(slidingPanel, panelWidth,
								TranslateAnimation.RELATIVE_TO_SELF, 0.75f,
								TranslateAnimation.RELATIVE_TO_SELF, 0.0f, 0,
								0.0f, 0, 0.0f);

					}
				}
			});

			/**
			 * ================================================================
			 * ==== == Setting image avatar
			 * **/
			Resources resources = this.getResources();
			Uri selectedImage = Uri
					.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
							+ "://"
							+ resources
									.getResourcePackageName(R.drawable.user_avatar)
							+ '/'
							+ resources
									.getResourceTypeName(R.drawable.user_avatar)
							+ '/'
							+ resources
									.getResourceEntryName(R.drawable.user_avatar));

			final ImageView imageView = (ImageView) findViewById(R.id.menuUserAvatar);
			Bitmap bitmap = mObjImage.getBitmap(selectedImage);
			bitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(),
					bitmap.getHeight(), true);

			// Setting rotate following rotation of image
			Matrix matrix = new Matrix();
			matrix.postRotate(0);
			Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
					bitmap.getWidth(), bitmap.getHeight(), matrix, true);

			// Set round corner
			Bitmap roundedBitmap = mObjImage.setRoundCorner(rotatedBitmap, 35);

			// Set image into imageview
			// imageView.setBackgroundResource(R.drawable.border_imageview);
			imageView.setImageBitmap(roundedBitmap);

			/**
			 * ================================================================
			 * ==== == Handle behavior for all button
			 * **/
			btnKatakama.setOnClickListener(this);
			btnHiragana.setOnClickListener(this);
			btnNhkWorld.setOnClickListener(this);
			btnSakura.setOnClickListener(this);
			btnVocabulary.setOnClickListener(this);
			btnDictionary.setOnClickListener(this);
			btnAccount.setOnClickListener(this);
			btnAboutUs.setOnClickListener(this);
			btnLogout.setOnClickListener(this);
			btnAdjs.setOnClickListener(this);
			btnVerbs.setOnClickListener(this);

			menuPanel.setOnTouchListener(new OnSwipeTouchUntil() {
				public boolean onSwipeTop() {
					Toast.makeText(MainActivity.this, "top", Toast.LENGTH_SHORT)
							.show();
					return true;
				}

				public boolean onSwipeRight() {
					Toast.makeText(MainActivity.this, "right",
							Toast.LENGTH_SHORT).show();
					return true;
				}

				public boolean onSwipeLeft() {
					Toast.makeText(MainActivity.this, "left",
							Toast.LENGTH_SHORT).show();
					return true;
				}

				public boolean onSwipeBottom() {
					Toast.makeText(MainActivity.this, "bottom",
							Toast.LENGTH_SHORT).show();
					return true;
				}
			});
		} catch (Exception ex) {
			Log.v(TAG, "onCreate :: " + ex.toString());
			mObjMessage.showMessageError(ConstantsUntil.MESSAGE_ERROR);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btnMenuAbout:
			callAboutUs();
			break;
		case R.id.btnMenuAccount:
			callAccount();
			break;
		case R.id.btnMenuDictionary:
			callDictionary();
			break;
		case R.id.btnMenuHiragana:
			callHiragana();
			break;
		case R.id.btnMenuKatakana:
			callKatakana();
			break;
		case R.id.btnMenuLogout:
			callLogout();
			break;
		case R.id.btnMenuNhkWorld:
			callNhkWorld();
			break;
		case R.id.btnMenuSakura:
			callSakuraCenter();
			break;
		case R.id.btnMenuVocabulary:
			callVocabulary();
			break;
		case R.id.btnMenuAdj:
			callAdjs();
			break;
		case R.id.btnMenuVerb:
			callVerbs();
			break;
		default:
			break;
		}
	}

	private void onLeftSwipe() {
		// Do something
		Toast.makeText(mContext, "Swipe left", Toast.LENGTH_SHORT).show();
	}

	private void onRightSwipe() {
		// Do something
	}

	/**
	 * Disable or Enable all control in view
	 * **/
	private void enableDisableView(View view, boolean enabled) {
		view.setEnabled(enabled);

		if (view instanceof ViewGroup) {
			ViewGroup group = (ViewGroup) view;

			for (int idx = 0; idx < group.getChildCount(); idx++) {
				enableDisableView(group.getChildAt(idx), enabled);
			}
		}
	}

	/**
	 * Function show screen katakana
	 * **/
	public void callKatakana() {
		try {
			Intent objIntent = new Intent(mContext, KatakanaActivity.class);
			mContext.startActivity(objIntent);
		} catch (Exception ex) {
			Log.v(TAG, "callKatakana :: " + ex.toString());
			mObjMessage.showMessageError(ConstantsUntil.MESSAGE_ERROR);
		}
	}

	/**
	 * Function show screen hiragana
	 * **/
	public void callHiragana() {
		try {
			Intent objIntent = new Intent(mContext, HiraganaActivity.class);
			mContext.startActivity(objIntent);
		} catch (Exception ex) {
			Log.v(TAG, "callHiragana :: " + ex.toString());
			mObjMessage.showMessageError(ConstantsUntil.MESSAGE_ERROR);
		}
	}

	/**
	 * Function show lesson from NHK World
	 * **/
	public void callNhkWorld() {
		try {
			Intent objIntent = new Intent(mContext, MainActivity.class);
			mContext.startActivity(objIntent);
		} catch (Exception ex) {
			Log.v(TAG, "callNhkWorld :: " + ex.toString());
			mObjMessage.showMessageError(ConstantsUntil.MESSAGE_ERROR);
		}
	}

	/**
	 * Function show lesson from sakura center
	 * **/
	public void callSakuraCenter() {
		try {
			Intent objIntent = new Intent(mContext, SakuraActivity.class);
			mContext.startActivity(objIntent);
		} catch (Exception ex) {
			Log.v(TAG, "callSakuraCenter :: " + ex.toString());
			mObjMessage.showMessageError(ConstantsUntil.MESSAGE_ERROR);
		}
	}

	/**
	 * Function show vocabulary screen
	 * **/
	public void callVocabulary() {
		try {
			Intent objIntent = new Intent(mContext, VocabularyActivity.class);
			mContext.startActivity(objIntent);
		} catch (Exception ex) {
			Log.v(TAG, "callVocabulary :: " + ex.toString());
			mObjMessage.showMessageError(ConstantsUntil.MESSAGE_ERROR);
		}
	}

	/**
	 * Funcrion show dictionary screen
	 * **/
	public void callDictionary() {
		try {
			Intent objIntent = new Intent(mContext, DictionaryActivity.class);
			mContext.startActivity(objIntent);
		} catch (Exception ex) {
			Log.v(TAG, "callDictionary :: " + ex.toString());
			mObjMessage.showMessageError(ConstantsUntil.MESSAGE_ERROR);
		}
	}

	/**
	 * Function show account screen
	 * **/
	public void callAccount() {
		try {
			Intent objIntent = new Intent(mContext, AccountActivity.class);
			mContext.startActivity(objIntent);
		} catch (Exception ex) {
			Log.v(TAG, "callAccount :: " + ex.toString());
			mObjMessage.showMessageError(ConstantsUntil.MESSAGE_ERROR);
		}
	}

	/**
	 * Function show about us screen
	 * **/
	public void callAboutUs() {
		try {
			Intent objIntent = new Intent(mContext, AboutActivity.class);
			mContext.startActivity(objIntent);
		} catch (Exception ex) {
			Log.v(TAG, "callAboutUs :: " + ex.toString());
			mObjMessage.showMessageError(ConstantsUntil.MESSAGE_ERROR);
		}
	}

	/**
	 * Funtion logout system
	 * **/
	public void callLogout() {
		try {
			Toast.makeText(mContext, "callLogout", Toast.LENGTH_SHORT).show();
		} catch (Exception ex) {
			Log.v(TAG, "callLogout :: " + ex.toString());
			mObjMessage.showMessageError(ConstantsUntil.MESSAGE_ERROR);
		}
	}

	/**
	 * Funtion verbs screen
	 * **/
	public void callVerbs() {
		try {
			Intent objIntent = new Intent(mContext, VerbsActivity.class);
			mContext.startActivity(objIntent);
		} catch (Exception ex) {
			Log.v(TAG, "callVerbs :: " + ex.toString());
			mObjMessage.showMessageError(ConstantsUntil.MESSAGE_ERROR);
		}
	}

	/**
	 * Funtion call Adjective screen
	 * **/
	public void callAdjs() {
		try {
			Intent objIntent = new Intent(mContext, AdjsActivity.class);
			mContext.startActivity(objIntent);
		} catch (Exception ex) {
			Log.v(TAG, "callAdjs :: " + ex.toString());
			mObjMessage.showMessageError(ConstantsUntil.MESSAGE_ERROR);
		}
	}
}
